package com.walk.zworld.ui.activity.my

import android.animation.ValueAnimator
import android.content.Intent
import android.os.Build
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.mvvm.vm.MyScoreActivityViewModel
import com.walk.zworld.view.WaveView
import com.base.lib.base.utils.MyMMKV.Companion.mmkv
import com.base.lib.base.view.BaseMvvmListActivity
import com.base.lib.httpUtils.UserScoreBean

class MyScoreActivity : BaseMvvmListActivity<UserScoreBean, MyScoreActivityViewModel, MyScoreActivity.ScoreAdapter>() {

    private var waveScore: WaveView? = null
    private var tvScore: TextView? = null

    override fun onBindAdapter(): Class<ScoreAdapter> = ScoreAdapter::class.java

    override fun onBindViewModel(): Class<MyScoreActivityViewModel>  = MyScoreActivityViewModel::class.java

    override fun initView() {
        val inflateView = View.inflate(mContext, R.layout.activity_score_list_header, null);
        waveScore = inflateView.findViewById<WaveView>(R.id.wave_score);
        tvScore = inflateView.findViewById<TextView>(R.id.tv_score);
        mAdapter.run {
            addHeaderView(inflateView)
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initData() {
        setTop("我的积分")
        setRight(R.drawable.paihangbang){
            startActivity(Intent(this, RankActivity::class.java))
        }
        waveScore?.run {
            setBorder(0, ContextCompat.getColor(this@MyScoreActivity, R.color.colorPrimary))
        }
        onRefreshEvent()
        if (mmkv.decodeInt(Constant.SCORE_UNM, 0) == 0) {
            getUserInfo()
        } else {
            doIntAnimator(tvScore, 0, mmkv.decodeInt(Constant.SCORE_UNM), 1000)
        }
    }

    private fun getUserInfo() {
        mViewModel.getUserInfo().observe(this, {
            mmkv.encode(Constant.SCORE_UNM, it.coinCount)
            doIntAnimator(tvScore, 0, it.coinCount, 1000)
        })
    }

    private fun getScoreList(page: Int) {
        //该接口page从1开始
        mViewModel.getScoreList(page + 1).observe(this, {
            it.datas.let { scoreList ->
                mAdapter.run {
                    if (page == 1) setList(scoreList)
                    else addData(scoreList)
                    if (it.over) loadMoreModule.loadMoreEnd()
                    else loadMoreModule.loadMoreComplete()
                }
            }
        })
    }

    override fun onResume() {
        super.onResume()
        waveScore?.start()
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onPause() {
        super.onPause()
        waveScore?.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        waveScore?.end()
    }

    class ScoreAdapter : BaseQuickAdapter<UserScoreBean, BaseViewHolder>(R.layout.item_score_record),
        LoadMoreModule {

        override fun convert(holder: BaseViewHolder, item: UserScoreBean) {
            holder.setText(R.id.tv_score_title, item.reason)
            holder.setText(R.id.tv_score_time, item.desc)
            holder.setText(R.id.tv_score_count, "+${item.coinCount}")
        }
    }

    private fun doIntAnimator(view: TextView?, start: Int, end: Int, duration: Long = 300) {
        val animator = ValueAnimator.ofInt(start, end)
        animator.run {
            addUpdateListener {
                view?.text = String.format("%d", it.animatedValue)
            }
            setDuration(duration)
            interpolator = DecelerateInterpolator()
            start()
        }
    }
}